package woaini.wind.bot.job.qylc;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.LocalDateTimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import woaini.wind.bot.api.holidays.HolidaysApi;
import woaini.wind.bot.api.holidays.dto.HolidaysResponseDayDTO;
import woaini.wind.bot.command.amap.api.AmapApi;
import woaini.wind.bot.command.amap.dto.WeatherDTO;
import woaini.wind.bot.core.event.segment.Messages;
import woaini.wind.bot.core.internal.Internal;
import woaini.wind.bot.core.manager.BotManager;
import woaini.wind.bot.job.utils.HolidaysTool;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.LinkedHashMap;

/**
 * QYLC工作
 * @see  woaini.wind.bot.job.qylc.QYLCWorkingJob
 * @author yefeng
 * {@date 2025-01-15 18:11:22}
 */
@Service(value = "QYLCWorkingJob")
@Slf4j
public class QYLCWorkingJob {

  @Value("${qylc.worker-group}")
  private String workerGroup;

  public void startWorking(){
    log.info("开始工作推送");

    //获取今天是不是节假日
    int year = LocalDate.now().getYear();
    LinkedHashMap<String, HolidaysResponseDayDTO> holidayInfos = HolidaysApi.getHolidays(year);;
    //获取今天是不是节假日
    HolidaysResponseDayDTO toDayHolidayInfo = holidayInfos.get(LocalDate.now().toString());
    //判断是否节假日
    boolean isHoliday = toDayHolidayInfo != null;
    //是否需要补班
    boolean isOffDay = toDayHolidayInfo != null && toDayHolidayInfo.getIsOffDay();
    //判断今天是不是周末
    boolean isWeekend = LocalDate.now().getDayOfWeek().getValue() > 5;
    //不是周末 和 不需要补班 就需要上班
    boolean needWork = !isWeekend && !isOffDay;
    if (needWork){
      String dayStartInfo = getDayStartInfo(isWeekend, isHoliday, toDayHolidayInfo, holidayInfos);
      Internal internal = BotManager.getBot("李风儿", "wx").internal();
      log.info(dayStartInfo);
      internal.sendGroupMessage(workerGroup, Messages.builder().text(dayStartInfo));
    }
  }

  public static String getDayStartInfo(boolean isWeekend,boolean toDayIsHoliday, HolidaysResponseDayDTO toDayHolidayInfo,
                                       LinkedHashMap<String, HolidaysResponseDayDTO> holidayInfos) {
    StringBuilder sb = new StringBuilder();

    //构建返回消息
    sb.append("早上好,现在是北京时间:").append(LocalDateTimeUtil.formatNormal(LocalDateTime.now())).append("\n");
    if (toDayIsHoliday){
      //判断是不是补班
      Boolean isOffDay = toDayHolidayInfo.getIsOffDay();
      if (!isOffDay){
        sb.append(STR."今天需要补\{toDayHolidayInfo.getName()}的班,记得起床上班哦");
      }else {
        sb.append(STR."今天是\{toDayHolidayInfo.getName()},不用上班哦");
      }
      sb.append("\n");
    }else if (isWeekend){
      //不是节假日判断是否是周末
      sb.append("今天是周末,不用上班哦");
      sb.append("\n");
    } else {
      //今天不是节假日 查找今年内下一个节假日
      HolidaysResponseDayDTO nextHolidayInfo = HolidaysTool.nextHolidayInfo(holidayInfos);
      if (nextHolidayInfo != null){
        //计算下一个节假日还有多少天
        String nextHolidayInfoDate = nextHolidayInfo.getDate();
        LocalDate nextHolidayInfoDateLocalDate = LocalDate.parse(nextHolidayInfoDate);
        //计算同今天差异的天数
        int betweenDays = LocalDate.now().until(nextHolidayInfoDateLocalDate).getDays();
        sb.append(STR."距离\{nextHolidayInfo.getName()}还有\{betweenDays}天哦\n");
      }
    }
    //查询日期天气
    WeatherDTO weather = AmapApi.weather("520115");
    boolean boolSuccess = weather.getStatus().equals("1");

    WeatherDTO.Lives lives = weather.getLives().get(0);
    if (boolSuccess) {
      sb.append(
        STR."""
            天气状况：\{lives.getWeather()}
            城市： \{lives.getCity()}
            实时气温： \{lives.getTemperature()}
            风向： \{lives.getWinddirection()}
            湿度： \{lives.getHumidity()}
        """);
      Integer temperature = Convert.toInt(lives.getTemperature());
      if (temperature <= 0){
        sb.append("0度以下哦,请注意保暖");
      }else if (temperature <= 10){
        sb.append("今天温度比较低,注意保暖哦");
      }else if (temperature <= 20){
        sb.append("还算暖和");
      }else {
        sb.append("今天温度有点热,注意防晒哦");
      }
    }
    return sb.toString();
  }
}
